package com.sztx.pay.center.rpc.service;

import java.util.List;

import org.mortbay.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.yhbf.YHBFSensitiveEnc;
import com.sztx.pay.center.dataaccess.dao.mysql.OrderTransferMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.PayFlowMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.UserBindCardMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.OrderTransferDO;
import com.sztx.pay.center.dataaccess.domain.PayFlowDO;
import com.sztx.pay.center.dataaccess.domain.UserBindCardDO;
import com.sztx.pay.center.rpc.api.service.SensitiveEncRpcService;
import com.sztx.se.core.service.impl.BaseServiceImpl;
@Service
public class SensitiveEncRpcServiceImpl extends BaseServiceImpl implements SensitiveEncRpcService{
	@Autowired
	UserBindCardMysqlDAO userBindCardMysqlDAO;
	@Autowired
	OrderTransferMysqlDAO orderTransferMysqlDAO;
	@Autowired
	PayFlowMysqlDAO payFlowMysqlDAO;
	@Override
	public void encSensitiveData() {
		List<UserBindCardDO> userBindCardDOS = userBindCardMysqlDAO.findAll(null);
		List<OrderTransferDO> orderTransferDOS = orderTransferMysqlDAO.findAll(null);
		List<PayFlowDO> payFlowDOS = payFlowMysqlDAO.findAll(null);
		for (UserBindCardDO userBindCardDO : userBindCardDOS) {
			userBindCardDO.setIdentityNo(YHBFSensitiveEnc.passDatasensitiveEnc(userBindCardDO.getIdentityNo(), "identityno"));
			//userBindCardDO.setRealName(YHBFSensitiveEnc.passDatasensitiveEnc(userBindCardDO.getRealName(), "realname"));
			userBindCardDO.setRealName(userBindCardDO.getRealName());
			userBindCardDO.setCardNo(YHBFSensitiveEnc.passDatasensitiveEnc(userBindCardDO.getCardNo(), "cardno"));
			userBindCardDO.setMobileNo(YHBFSensitiveEnc.passDatasensitiveEnc(userBindCardDO.getMobileNo(), "mob"));
			Log.info("encSensitiveData  userBindCardDO="+JSONObject.toJSONString(userBindCardDO));
			userBindCardMysqlDAO.updateBindCard(userBindCardDO, userBindCardDO.getBindCardNo());
		}
		for (OrderTransferDO orderTransferDO : orderTransferDOS) {
			orderTransferDO.setPayeeCardNo(YHBFSensitiveEnc.passDatasensitiveEnc(orderTransferDO.getPayeeCardNo(), "cardno"));
			//orderTransferDO.setCardHolder(YHBFSensitiveEnc.passDatasensitiveEnc(orderTransferDO.getCardHolder(), "realname"));
			orderTransferDO.setPayeeCardHolder(orderTransferDO.getPayeeCardHolder());
			orderTransferMysqlDAO.update(orderTransferDO);
			Log.info("encSensitiveData  orderTransferDO="+JSONObject.toJSONString(orderTransferDO));
		}
		for (PayFlowDO payFlowDO : payFlowDOS) {
			payFlowDO.setPayerBankAct(YHBFSensitiveEnc.passDatasensitiveEnc(payFlowDO.getPayerBankAct(), "cardno"));
			payFlowDO.setPayerMobile(YHBFSensitiveEnc.passDatasensitiveEnc(payFlowDO.getPayerMobile(), "mob"));
			payFlowDO.setPayerIdentityNo(YHBFSensitiveEnc.passDatasensitiveEnc(payFlowDO.getPayerIdentityNo(), "identityno"));
			payFlowMysqlDAO.updateEnc(payFlowDO);
			Log.info("encSensitiveData  payFlowDO="+JSONObject.toJSONString(payFlowDO));
		}
	}
	@Override
	public void decSensitiveData() {
		List<UserBindCardDO> userBindCardDOS = userBindCardMysqlDAO.findAll(null);
		List<OrderTransferDO> orderTransferDOS = orderTransferMysqlDAO.findAll(null);
		List<PayFlowDO> payFlowDOS = payFlowMysqlDAO.findAll(null);
		for (UserBindCardDO userBindCardDO : userBindCardDOS) {
			userBindCardDO.setIdentityNo(YHBFSensitiveEnc.sensitiveDec(userBindCardDO.getIdentityNo(), "identityno"));
			//userBindCardDO.setRealName(YHBFSensitiveEnc.sensitiveDec(userBindCardDO.getRealName(), "realname"));
			userBindCardDO.setRealName(userBindCardDO.getRealName());
			userBindCardDO.setCardNo(YHBFSensitiveEnc.sensitiveDec(userBindCardDO.getCardNo(), "cardno"));
			userBindCardDO.setMobileNo(YHBFSensitiveEnc.sensitiveDec(userBindCardDO.getMobileNo(), "mob"));
			Log.info("decSensitiveData  userBindCardDO="+JSONObject.toJSONString(userBindCardDO));
			userBindCardMysqlDAO.updateBindCard(userBindCardDO, userBindCardDO.getBindCardNo());
		}
		for (OrderTransferDO orderTransferDO : orderTransferDOS) {
			orderTransferDO.setPayeeCardNo(YHBFSensitiveEnc.sensitiveDec(orderTransferDO.getPayeeCardNo(), "cardno"));
			//orderTransferDO.setCardHolder(YHBFSensitiveEnc.sensitiveDec(orderTransferDO.getCardHolder(), "realname"));
			orderTransferDO.setPayeeCardHolder(orderTransferDO.getPayeeCardHolder());
			orderTransferMysqlDAO.update(orderTransferDO);
			Log.info("decSensitiveData  orderTransferDO="+JSONObject.toJSONString(orderTransferDO));
		}
		for (PayFlowDO payFlowDO : payFlowDOS) {
			payFlowDO.setPayerBankAct(YHBFSensitiveEnc.sensitiveDec(payFlowDO.getPayerBankAct(), "cardno"));
			payFlowDO.setPayerMobile(YHBFSensitiveEnc.sensitiveDec(payFlowDO.getPayerMobile(), "mob"));
			payFlowDO.setPayerIdentityNo(YHBFSensitiveEnc.sensitiveDec(payFlowDO.getPayerIdentityNo(), "identityno"));
			payFlowMysqlDAO.updateEnc(payFlowDO);
			Log.info("decSensitiveData  payFlowDO="+JSONObject.toJSONString(payFlowDO));
		}
	}
	
}
